<?php

class openapi_api_function_v1_aftersales extends openapi_api_function_abstract implements openapi_api_function_interface{

	public function gorefund($params,&$code,&$sub_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $order_info = app::get('b2c')->model('orders')->getRow("*",array('order_id'=>$params['order_id']));
        if($order_info['refund_status']!='0' && $order_info['refund_status']!='2'){
        	return $this->apiData(false,null,400,app::get('b2c')->_('您已提交过申请'));
        }
		if ($params['has_receipt']) {//已收到货
			if($params['is_need_return']){
				//需要退货退款
				$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_add($params['order_id'],1,$err_msg);
				if(!$res){
					return $this->apiData(false,null,400,$err_msg);
				}
				$result['type'] = 1;
				$result['reason'] = array(
					'七天无理由退换货','商品质量问题','收到的商品不符','退运费','发票问题','收到假货','其他'
					);
			}else{
				//不需要退货退款
				$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->gorefund_mai_3($params['order_id'],$err_msg);
				if(!$res){
					return $this->apiData(false,null,400,$err_msg);
				}
				$result['type'] = 3;
				$result['reason'] = array(
					'商品质量问题','收到的商品不符','退运费','发票问题','其他'
					);
			}	
		}else{
			//未收到货退款
			$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->gorefund_mai_4($params['order_id'],$err_msg);
			if(!$res){
				return $this->apiData(false,null,400,$err_msg);
			}
			$result['type'] = 4;
			$result['reason'] = array(
				'协商一致退款','未按约定时间发货','虚假发货','其他'
				);
		}
		$products = array();
		foreach ($res['order']['items'] as $key => $items) {
			$item = array();
			$item['goods_id'] = $items['goods_id'];
			$item['bn'] = $items['bn'];
			$item['name'] = $items['name'];
			$item['amount'] = $items['amount'];
			$item['quantity'] = $items['quantity'];
			$item['price'] = $items['price'];
			$default_image = app::get('b2c')->model('goods')->getList("image_default_id",array('goods_id'=>$items['goods_id']));
			$default_image_url = kernel::single('openapi_utils_image')->getimgbyid($default_image[0]['image_default_id']);
			$item['image_url'] = empty($default_image_url['m_url'])?$default_image_url['url']:$default_image_url['m_url'];
			$products[] = $item;
		}
		$result['order_id'] = $res['order_id'];
		$result['products'] = $products;
		$result['cost_shipping'] = $res['order']['shipping']['cost_shipping'];

		if($order_info['discount_value'] > 0){
            $result['max_return_price'] = $order_info['payed']+ $order_info['discount_value'];
        }else{
           $result['max_return_price'] = $order_info['payed'];
        }
		return $this->apiData(true,$result,200);
	}

	//退货退款申请
	public function return_save($params,&$code,&$err_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        if($params['return_type']=='1'){
        	//退货退款
        	$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_save($params,$err_msg);
        	if(!$res){
				return $this->apiData(false,null,400,$err_msg);
			}
        }else{
        	//无需退货的退款申请
        	$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_save_mai($params,$err_msg);
        	if(!$res){
				return $this->apiData(false,null,400,$err_msg);
			}
        }
		return $this->apiData(true,null,200,$res);
	}
	public function goreship($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $return = array();
        $dlycorp = app::get('b2c')->model('dlycorp');
        $return['corplist'] = $dlycorp->getList('*');
        //获取收货地址
        $returns = app::get('aftersales')->model('return_product');
        $refund_address = $returns->getRow('refund_address,return_id,close_time,seller_comment',array('order_id'=>$params['order_id'],'status'=>'3','refund_type'=>'2'));
        // $this->pagedata['return_id'] = $refund_address['return_id'];
        $obj_address = app::get('business')->model('dlyaddress');
        $address = $obj_address->getList('*',array('da_id'=>$refund_address['refund_address']));
        $regions = explode(':',$address[0]['region']);
        $region = $regions[1];
        $return['refund_address'] = $region.'/'.$address[0]['address'].'，'.$address[0]['uname'].'，'.$address[0]['phone'];
        $return['addr_id'] = $refund_address['refund_address'];
        $return['seller_comment'] = $refund_address['seller_comment'];
        $return['return_id'] = $refund_address['return_id'];
        // $this->pagedata['time'] = ($refund_address['close_time'])*1000;
        // $this->pagedata['now_time'] = time()*1000;
        // $this->pagedata['order_id'] = $order_id;
        // $this->output('aftersales');
        return $this->apiData(true,$return,200,$err_msg);
	}
	public function reshipped($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		$params['member_id'] = $member_id;
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->reshipped($params,$err_msg);
        if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,null,200,$err_msg);
	}
	public function myrefund($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		$npage = $params['npage']?$params['npage']:1;
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
		$clos = "return_id,order_id,add_time,status,member_id,refund_type,is_intervene";
        $filter = array();
        $filter["member_id"] = $member_id;

        $obj_return_policy = kernel::service("aftersales.return_policy");
        $arr_settings = array();
        $aData = $obj_return_policy->get_return_product_list($clos, $filter, $npage);
        $obj_account = app::get('pam')->model('account');
        //添加用户名
        foreach($aData['data'] as $key=>$val){
            $uname = $obj_account->getRow('login_name',array('account_id'=>$val['member_id']));
            $aData['data'][$key]['uname'] = $uname['login_name'];
            $aData['data'][$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);

            if($val['is_intervene']=='1'){
                if($val['status']=='3' && $val['refund_type']=='2'){
                    $aData['data'][$key]['btn'] = 'fill';
                }else if(($val['status']=='14'||$val['status']=='11') && $val['refund_type']=='2'){
                    $aData['data'][$key]['btn'] = 'modify';
                }else if($val['status']=='11' && $val['refund_type']!='2'){
                    $aData['data'][$key]['btn'] = 'modify';
                }else{
                    $aData['data'][$key]['btn'] = 'show';
                }
            }else{
                $aData['data'][$key]['btn'] = 'show';
            }
            
            
            if($val['is_intervene'] == '1'){
                if($val['status'] == '1'){
                    $aData['data'][$key]['status'] = '退款协议等待卖家确认';
                }else if($val['status'] == '2'){
                    $aData['data'][$key]['status'] = '审核中';
                }else if($val['status'] == '3'){
                    $aData['data'][$key]['status'] = '审核通过';
                }else if($val['status'] == '4'){
                    $aData['data'][$key]['status'] = '完成';
                }else if($val['status'] == '5'){
                    $aData['data'][$key]['status'] = '审核未通过';
                }else if($val['status'] == '6'){
                    $aData['data'][$key]['status'] = '已退款';
                }else if($val['status'] == '10'){
                    $aData['data'][$key]['status'] = '已取消';
                }else if($val['status'] == '11'){
                    $aData['data'][$key]['status'] = '待修改';
                }else if($val['status'] == '12'){
                    $aData['data'][$key]['status'] = '买家已退货，等待卖家确认';
                }else if($val['status'] == '13'){
                    $aData['data'][$key]['status'] = '已修改';
                }else if($val['status'] == '14'){
                    $aData['data'][$key]['status'] = '卖家收到退货，拒绝退款';
                }else if($val['status'] == '15'){
                    $aData['data'][$key]['status'] = '卖家同意退款，等待卖家打款至平台';
                }else if($val['status'] == '16'){
                    $aData['data'][$key]['status'] = '卖家已退款，等待系统结算';
                }
            }else if($val['is_intervene'] == '2'){
                $aData['data'][$key]['status'] = '申请平台介入中，等待卖家举证';
            }else if($val['is_intervene'] == '3'){
                $aData['data'][$key]['status'] = '平台处理中';
            }else{
                $aData['data'][$key]['status'] = '平台已处理';
            }

        }
        if (isset($aData['data']) && $aData['data']){
            return $this->apiData(true,$aData['data'],200);
		}
        return $this->apiData(false,null,400,app::get('b2c')->_('您还没有退款退货信息！'));
	}
	public function subrefund($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		$npage = $params['npage']?$params['npage']:1;
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
		$clos = "return_id,order_id,add_time,status,member_id,is_intervene,refund_type";
        $filter = array();

        //添加过滤条件
        $storeinfo = app::get('business')->model('storemanger')->getRow('store_id',array('account_id'=>$member_id));
        $filter["store_id"] = $storeinfo['store_id'];
        
        $obj_return_policy = kernel::service("aftersales.return_policy");

        $aData = $obj_return_policy->get_return_product_list($clos, $filter, $npage);
        $obj_account = app::get('pam')->model('account');
        $order_obj = app::get('b2c')->model('orders');
        //添加用户名
        foreach($aData['data'] as $key=>$val){
            $uname = $obj_account->getRow('login_name',array('account_id'=>$val['member_id']));
            $aData['data'][$key]['uname'] = $uname['login_name'];
            $status = $order_obj->dump(array('order_id'=>$val['order_id']),'refund_status');
            $aData['data'][$key]['refund_status'] = $status['refund_status'];
            $aData['data'][$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);

            if($val['is_intervene']=='1'){
                if($val['status']=='1'){
                    $aData['data'][$key]['btn'] = 'do_apply';//处理申请
                }else if($val['status']=='2'){
                    $aData['data'][$key]['btn'] = 'show';//查看
                }else if($val['status']=='12'){
                    if($val['refund_status']=='5'){
                        $aData['data'][$key]['btn'] = 'confirm';//确认收货
                    }else{
                        $aData['data'][$key]['btn'] = 'show';//查看
                    }
                }else{
                    $aData['data'][$key]['btn'] = 'show';//查看
                }
            }else if($val['is_intervene']=='2' && $status['refund_status']=='8'){
                $aData['data'][$key]['btn'] = 'evidence';//举证
            }else{
                $aData['data'][$key]['btn'] = 'show';//查看
            }
            
            if($val['is_intervene'] == '1'){
                if($val['status'] == '1'){
                    $aData['data'][$key]['status'] = '退款协议等待卖家确认';
                }else if($val['status'] == '2'){
                    $aData['data'][$key]['status'] = '审核中';
                }else if($val['status'] == '3'){
                    $aData['data'][$key]['status'] = '审核通过';
                }else if($val['status'] == '4'){
                    $aData['data'][$key]['status'] = '完成';
                }else if($val['status'] == '5'){
                    $aData['data'][$key]['status'] = '审核未通过';
                }else if($val['status'] == '6'){
                    $aData['data'][$key]['status'] = '已退款';
                }else if($val['status'] == '10'){
                    $aData['data'][$key]['status'] = '已取消';
                }else if($val['status'] == '11'){
                    $aData['data'][$key]['status'] = '待修改';
                }else if($val['status'] == '12'){
                    $aData['data'][$key]['status'] = '买家已退货，等待卖家确认';
                }else if($val['status'] == '13'){
                    $aData['data'][$key]['status'] = '已修改';
                }else if($val['status'] == '14'){
                    $aData['data'][$key]['status'] = '卖家收到退货，拒绝退款';
                }else if($val['status'] == '15'){
                    $aData['data'][$key]['status'] = '卖家同意退款，等待卖家打款至平台';
                }else if($val['status'] == '16'){
                    $aData['data'][$key]['status'] = '卖家已退款，等待系统结算';
                }
            }else if($val['is_intervene'] == '2'){
                $aData['data'][$key]['status'] = '申请平台介入中，等待卖家举证';
            }else if($val['is_intervene'] == '3'){
                $aData['data'][$key]['status'] = '平台处理中';
            }else{
                $aData['data'][$key]['status'] = '平台已处理';
            }
        }
        if (isset($aData['data']) && $aData['data']){
            return $this->apiData(true,$aData['data'],200);
		}
        return $this->apiData(false,null,400,app::get('b2c')->_('下级还没有退款退货信息！'));
	}
	public function return_details($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
		$res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_details($params,$err_msg);
		if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,$res,200);
	}
	public function do_return($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->do_return($params['order_id'],$params['return_type'],$err_msg);
		if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,$res,200);
	}

	/**
	 * 同意退货退款
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function do_agree_return($params,&$code,&$err_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->do_agree_return($params,$err_msg);
		if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,$res,200,$err_msg);
	}
    /**
     * 拒绝退货退款
     * @param
     * @return
     * @author hmg
     **/
	public function do_noagree_return($params,&$code,&$err_msg){
		$member_id = $this->checklogin($params['login_token']);
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_edit_seller($params,$err_msg);
		if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,$res,200,$err_msg);
	}

	/**
	 * 确认收到退货
	 * @param
	 * @return
	 * @author hmg
	 **/
	public function send_finish($params,&$code,&$err_msg)
	{
		$member_id = $this->checklogin($params['login_token']);
		if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->send_finish($params,$err_msg);
		if(!$res){
			return $this->apiData(false,null,400,$err_msg);
		}
		return $this->apiData(true,$res,200,$err_msg);
	}
    /**
     * 撤销申请
     * @param
     * @return
     * @author hyf
     **/
    public function return_refuse($params,&$code,&$err_msg){
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->return_refuse($params,$err_msg);
        return $this->apiData(true,null,200,$err_msg);
    }
    /**
     * 平台介入申请
     * @param
     * @return
     * @author hyf
     **/
    public function intereven($params,&$code,&$err_msg){
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->intereven($params,$err_msg);
        return $this->apiData(true,null,200,$err_msg);
    }
    /**
     * 编辑退货退款申请
     * @param
     * @return
     * @author hyf
     **/
    public function edit_refund($params,&$code,&$err_msg){
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $order_id = $params['order_id'];
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->edit_refund_2($order_id,$err_msg);
        if(!$res){
            return $this->apiData(false,null,400,$err_msg);
        }
        $products = array();
        foreach ($res['order']['items'] as $key => $items) {
            $item = array();
            $item['goods_id'] = $items['goods_id'];
            $item['bn'] = $items['bn'];
            $item['name'] = $items['name'];
            $item['amount'] = $items['amount'];
            $item['quantity'] = $items['quantity'];
            $item['price'] = $items['price'];
            $default_image = app::get('b2c')->model('goods')->getList("image_default_id",array('goods_id'=>$items['goods_id']));
            $default_image_url = kernel::single('openapi_utils_image')->getimgbyid($default_image[0]['image_default_id']);
            $item['image_url'] = empty($default_image_url['m_url'])?$default_image_url['url']:$default_image_url['m_url'];
            $products[] = $item;
        }
        $result['order_id'] = $res['order_id'];
        $result['products'] = $products;
        $result['cost_shipping'] = $res['order']['shipping']['cost_shipping'];

        if($res['order']['discount_value'] > 0){
            $result['max_return_price'] = $res['order']['payed']+ $res['order']['discount_value'];
        }else{
           $result['max_return_price'] = $res['order']['payed'];
        }
        if ($res['return_item']['refund_type']=='2') {
            $result['type'] = 1;
            $result['reason'] = array(
                '七天无理由退换货','商品质量问题','收到的商品不符','退运费','发票问题','收到假货','其他'
            );
        }
        if($res['return_item']['refund_type']=='3'){
            $result['type'] = 3;
            $result['reason'] = array(
                '商品质量问题','收到的商品不符','退运费','发票问题','其他'
                );
        }
        if($res['return_item']['refund_type']=='4'){
            $result['type'] = 4;
            $result['reason'] = array(
                '协商一致退款','未按约定时间发货','虚假发货','其他'
                );
        }
        $result['seller_reason'] = $res['return_item']['seller_reason'];
        if(!empty($res['return_item']['content'])){
            $result['content'] = $res['return_item']['content'];
        }
        $result['comment'] = $res['return_item']['comment'];
        if(!empty($res['return_item']['image_file'])){
            $result['image_file'] = base_storager::image_path($res['return_item']['image_file'],'s');
        }
        if(!empty($res['return_item']['image_file1'])){
            $result['image_file1'] = base_storager::image_path($res['return_item']['image_file1'],'s');
        }
        if(!empty($res['return_item']['image_file2'])){
            $result['image_file2'] = base_storager::image_path($res['return_item']['image_file2'],'s');
        }

        return $this->apiData(true,$result,200,$err_msg);
    }

    /**
    * 同意未发货时退款申请
    * @param
    * @return
    * @author hmg
    **/
    public function do_agree($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->do_agree($params,$err_msg);
        if(!$res){
            return $this->apiData(false,null,400,$err_msg);
        }
        return $this->apiData(true,$res,200,$err_msg);
    }

    /**
    * 拒绝退款继续发货
    * @param
    * @return
    * @author hmg
    **/
    public function do_send($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->do_send($params,$err_msg);
        if(!$res){
            return $this->apiData(false,null,400,$err_msg);
        }
        return $this->apiData(true,$res,200,$err_msg);
    }

    /**
    * 售后申请
    * @param
    * @return
    * @author hmg
    **/
    public function safeguard_add($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->safeguard_add($params,$err_msg);
        if(!$res){
            return $this->apiData(false,null,400,$err_msg);
        }
        return $this->apiData(true,kernel::single('openapi_utils_datahelper')->array_merge_fb($res),200,$err_msg);
    }
    /**
    * 获得卖家举证页面数据
    * @param
    * @return
    * @author hyf
    **/
    public function seller_update($params,&$code,&$sub_msg){
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $return_id = $params['return_id'];
        $rp = app::get('aftersales')->model('return_product');
        $objOrder = app::get('b2c')->model('orders');
        $obj_return_policy = kernel::single('aftersales_data_return_policy');
        $return_item =  $obj_return_policy->get_return_product_by_return_id($return_id);

        //添加退款日志
        $obj_return_log = app::get('aftersales')->model('return_log');
        $log_info = $obj_return_log->getList('*',array('order_id'=>$return_item['order_id']),0,-1,'alttime DESC');
        foreach ($log_info as $key => $value) {
            $log_info[$key]['alttime'] = date('Y-m-d H:i:s',$value['alttime']);
            if($value['role'] == 'member'){
                $log_info[$key]['nick'] = app::get('b2c')->_('我：');
            }else if($value['role'] == 'seller'){
                $log_info[$key]['nick'] = app::get('b2c')->_('卖家：');
            }else{
                $log_info[$key]['nick'] = app::get('b2c')->_('管理员：');
            }
        }
        return $this->apiData(true,$log_info,200,$err_msg);
    }
    /**
    * 提交卖家举证
    * @param
    * @return
    * @author hyf
    **/
    public function seller_intereven($params,&$code,&$sub_msg){
        $member_id = $this->checklogin($params['login_token']);
        if(empty($member_id)){
            return $this->apiData(false,null,301,app::get('b2c')->_('请登录'));
        }
        $params['member_id'] = $member_id;
        $res = kernel::single('aftersales_api_site_business',app::get('aftersales'))->seller_intereven($params,$err_msg);
        if(!$res){
            return $this->apiData(false,null,400,$err_msg);
        }
        return $this->apiData(true,null,200,$err_msg);
    }

}